package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskClassRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;

/**
 *
 */
@CrossOrigin
@RestController
public class WorkcorrectUpdController extends YxyApiBaseController {

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    HomeworkTaskClassRepository homeworkTaskClassRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workcorrect/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        if (reqModel.taskReportQuesId == null || reqModel.taskReportQuesId.equals("")) {
            Assert.isTrue(false, "未找到学生相关通知作业，请重试！");
        }
        // 获取用户指定的答题
        HomeworkTaskReportQues homeworkTaskReportQues = homeworkTaskReportQuesRepository
                .findOne(reqModel.taskReportQuesId);
        // 判断是否存在数据
        if (homeworkTaskReportQues == null) {
            Assert.isTrue(false, "未找到学生相关通知作业，请重试！");
        } else {
            // 获取学生作业记录
            HomeworkTaskReport homeworkTaskReport = homeworkTaskReportRepository.findTop1ByTaskIdAndClassIdAndStudentId(
                    homeworkTaskReportQues.getTaskId(), homeworkTaskReportQues.getClassId(),
                    homeworkTaskReportQues.getStudentId());
            // 获取班级任务
            HomeworkTaskClass homeworkTaskClassz = homeworkTaskClassRepository.findTop1ByTaskIdAndClassId(
                    homeworkTaskReportQues.getTaskId(), homeworkTaskReportQues.getClassId());
            // save
            try {
                // 教师批阅学生通知类试题
                homeworkTaskReportQues.setNotifyPassed(reqModel.score);
                homeworkTaskReportQuesRepository.save(homeworkTaskReportQues);
                // 更新学生任务完成状态为 -> 批阅
                homeworkTaskReport.setStatusOfFinished(2);
                homeworkTaskReportRepository.save(homeworkTaskReport);
                // 更新班级批阅人数
                homeworkTaskClassz
                        .setMarkingOfStudent(homeworkTaskReportRepository.countByClassIdAndTaskIdAndStatusOfFinished(
                                homeworkTaskReportQues.getClassId(), homeworkTaskReportQues.getTaskId(), 2));
                homeworkTaskClassRepository.save(homeworkTaskClassz);
            } catch (Exception e) {
                Assert.isTrue(false, "批阅学生作业发生错误，请重试!");
            }
        }
        return respModel;
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 任务学生试题报告Id
        public String taskReportQuesId;
        // 班级Id
        public String classId;
        // 分数
        public Integer score;
        // 用户Id (不用传参)
        public String userId;
        // 学校Id (不用传参)
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
    }

    public static class ClassStuTaskReport {
    }
}
